package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.beauty.domain.BeautyVehicle;
import com.linq.cool.beauty.dto.BeautyVehicleQueryDTO;
import com.linq.cool.beauty.service.BeautyVehicleService;
import com.linq.cool.beauty.vo.BeautyVehicleVO;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.RegexUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/1 04:01
 * @Description: 客户车辆
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("vehicle")
public class BeautyVehicleController extends BaseController {
    @Autowired
    private BeautyVehicleService beautyVehicleService;

    @PreAuthorize(hasPerm = "beauty:vehicle:list")
    @GetMapping("list")
    public Result<?> list(BeautyVehicleQueryDTO dto) {
        IPage<BeautyVehicleVO> page = beautyVehicleService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }


    @GetMapping(value = "{vehicleId}")
    public Result<?> getInfo(@PathVariable("vehicleId") Long vehicleId) {
        return Result.ok(beautyVehicleService.getById(vehicleId));
    }

    @Log(moduleTitle = "车辆管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:vehicle:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyVehicle vehicle) {
        if (!RegexUtils.checkLicenseNumber(vehicle.getLicenseNumber())) {
            return Result.fail("车牌号不合法");
        }
        if (!RegexUtils.checkFrameNo(vehicle.getFrameNo())) {
            return Result.fail("车架号不合法");
        }
        if (!RegexUtils.checkEngineNo(vehicle.getEngineNo())) {
            return Result.fail("发动机号不合法");
        }
        return this.toResult(beautyVehicleService.saveVehicle(vehicle));
    }

    @Log(moduleTitle = "车辆管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:vehicle:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody BeautyVehicle vehicle) {
        if (!RegexUtils.checkLicenseNumber(vehicle.getLicenseNumber())) {
            return Result.fail("车牌号不合法");
        }
        if (!RegexUtils.checkFrameNo(vehicle.getFrameNo())) {
            return Result.fail("车架号不合法");
        }
        if (!RegexUtils.checkEngineNo(vehicle.getEngineNo())) {
            return Result.fail("发动机号不合法");
        }
        return this.toResult(beautyVehicleService.updateVehicle(vehicle));
    }


    @Log(moduleTitle = "车辆管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:vehicle:remove")
    @DeleteMapping("{vehicleIds}")
    public Result<?> remove(@PathVariable("vehicleIds") List<Long> vehicleIds) {
        return this.toResult(beautyVehicleService.removeByIds(vehicleIds));
    }
}
